using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class NguoiDungDA : DataAccessBase
    {
		#region Constructors
		
		public NguoiDungDA() : base()
        {
			_selectSql = "Select * From NGUOIDUNG";
			_insertSql = "Insert Into NGUOIDUNG(MAND,MATKHAU,HOTENND,LOAIND,MAHK) Values (@MAND,@MATKHAU,@HOTENND,@LOAIND,@MAHK)";
        	_updateSql = "Update NGUOIDUNG Set MATKHAU=@MATKHAU,HOTENND=@HOTENND,LOAIND=@LOAIND,MAHK=@MAHK Where 1=1 And MAND=@MAND";
        	_deleteSql = "Delete From NGUOIDUNG Where 1=1 And MAND=@MAND";
        	_primarSql = "Select * From NGUOIDUNG Where 1=1 And MAND=@MAND";
        }
        public string GetInsertText
        {
            get { return _insertSql; }
        }
		protected static NguoiDungDA _inst = null;
		public static NguoiDungDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new NguoiDungDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private NguoiDung GetCurrent(IDataReader reader, bool isDeep)
		{
			NguoiDung ret = new NguoiDung();
			ret.MaNguoiDung = (string)reader["MAND"];
			ret.MatKhau = (string)reader["MATKHAU"];
			ret.HoTenND = (string)reader["HOTENND"];
			ret.LoaiND = (LOAI_NGUOI_DUNG)(int)reader["LOAIND"];
			ret.MaHanhKhach = (string)reader["MAHK"];			
			return ret;
		}

		public List<NguoiDung> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<NguoiDung> ret = new List<NguoiDung>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}
		
		public NguoiDung GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MAND", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}

        public NguoiDung Login(string User,string Pass)
        {
            var strSQL = "select * from NGUOIDUNG where MAND=@MAND and MATKHAU=@MATKHAU";
            DbCommand cmd = GetCommand(strSQL);            
            SetParam(cmd, "@MAND", User);
            SetParam(cmd, "@MATKHAU", Pass);
            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
                    if (dr.Read())
                    {
                        return GetCurrent(dr, false);
                    }
                    return null;
                }
            }
        }
        public DbCommand GetCommInsert(NguoiDung entity)
        {
            DbCommand cmd = GetCommand(_insertSql);
            SetParam(cmd, "@MAND", entity.MaNguoiDung);
            SetParam(cmd, "@MATKHAU", entity.MatKhau);
            SetParam(cmd, "@HOTENND", entity.HoTenND);
            SetParam(cmd, "@LOAIND", (int)entity.LoaiND);
            SetParam(cmd, "@MAHK", entity.MaHanhKhach);
            return cmd;
        }
		public bool Insert(NguoiDung entity)
		{
            DbCommand cmd = GetCommInsert(entity);			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Update(NguoiDung entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MAND", entity.MaNguoiDung);
            SetParam(cmd, "@MATKHAU", entity.MatKhau);
            SetParam(cmd, "@HOTENND", entity.HoTenND);
            SetParam(cmd, "@LOAIND",(int) entity.LoaiND);
            SetParam(cmd, "@MAHK", entity.MaHanhKhach);

			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		public bool Delete(NguoiDung entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MAND", entity.MaNguoiDung);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}

		
		#endregion
    }
}


